home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / scrmem.com / SCRMEM.C
Encoding:
C/C++ Source or Header  |  1990-01-28  |  7.7 KB  |  162 lines

  1. /*****************************************************************************\
  2. *                                                                             *
  3. * Programmed By : Robert Goshko       CIS : [76645,3335]                      *
  4. *                                                                             *
  5. * These three functions allow you to write directly to the screen.  Something *
  6. * you must do, in order to use the extended text modes supported by EGA and   *
  7. * VGA cards alike.                                                            *
  8. *                                                                             *
  9. * The first function, sprint, is almost identical to cprintf, except there    *
  10. * are five control variables before the string to be printed, they are:       *
  11. *                                                                             *
  12. * sprint( xloc, ylox, fgcol, bgcol, blink flag, string...                     *
  13. *      xloc - the x location on the screen.                                   *
  14. *      yloc - the y location on the screen.                                   *
  15. *      fgcol - the foreground color (0-15).                                   *
  16. *      bgcol - the background color (0-7).                                    *
  17. *      blink flag - 0 for non-blinking text, 1 for blinking text.             *
  18. *      string - the string you want printed eg. (" Balance = %5.2d ",bal)     *
  19. *                                                                             *
  20. * The second two are used to save and restore the screen much like the        *
  21. * gettext and puttext functions, except now you can access past line 25 of    *
  22. * the standard screen.                                                        *
  23. *                                                                             *
  24. * Freeware from Axis Development                                              *
  25. *                                                                             *
  26. \*****************************************************************************/
  27.  
  28. #include <stdarg.h>
  29.  
  30. #define SCR_SEG     0xB800  /* Segment of video memory                   */
  31. #define COL        80      /* Number of columns on screen               */
  32. #define ROW        50      /* Number of rows on screen (43 EGA/50 VGA)  */
  33. #define NULL        0x00    /* Null Flag                                 */
  34.  
  35. int sprint(int x, int y, int f, int b, int bl, char *fmt, ... )
  36. {
  37.    void far *daddress;                  /* Physical Address of Data          */
  38.    void far *aaddress;                  /* Physical Address of Attribute     */
  39.    unsigned buf_seg, buf_off, attr_seg, attr_off;
  40.    va_list  argptr;            /* Argument list pointer         */
  41.    char str[140];            /* Buffer to build sting into         */
  42.    int i, scr_off, attr, cnt;
  43.  
  44.    if( x < 1 || x > COL )               /* Check for legal screen values     */
  45.       return(-1);
  46.    if( y < 1 || y > ROW )
  47.       return(-1);
  48.  
  49.    va_start( argptr, format );        /* Initialize va_ functions         */
  50.    cnt = vsprintf( str, fmt, argptr );    /* prints string to buffer         */
  51.    va_end( argptr );            /* Close va_ functions             */
  52.  
  53.    daddress = ( void far *)str;         /* Determine physical address (data) */
  54.    buf_seg = FP_SEG(daddress);          /* Get segment (data)                */
  55.    buf_off = FP_OFF(daddress);          /* Get offset (data)                 */
  56.  
  57.    scr_off = ( ( x * 2 ) - 2 ) + ( ( y * ( 2 * COL ) ) - ( 2 * COL ) );
  58.                     /* Compute x & y location in memory  */
  59.  
  60.    attr = 0;
  61.  
  62.    if( bl == 1 )                        /* Check for blink                   */
  63.       attr += 128;                         /* If blink then set bit 8        */
  64.    if( b > -1 && b < 8 )                /* Check for valid background color  */
  65.       attr = attr + ( 16 * b );            /* If valid, set color            */
  66.    else                                 /* If not a valid background color   */
  67.       return(-1);                          /* Return -1                      */
  68.    if( f > -1 && f < 16 )               /* Check for valid foreground color  */
  69.       attr = attr + f;                     /* If valid, set color            */
  70.    else                                 /* If not a valid foreground color   */
  71.       return(-1);                          /* Return -1                      */
  72.  
  73.    aaddress = (void far *)&attr;        /* Determine phisical address (attr) */
  74.    attr_seg = FP_SEG(aaddress);         /* Get segment (attr)                */
  75.    attr_off = FP_OFF(aaddress);         /* Get offset (attr)                 */
  76.  
  77.    for( i = 0 ; i < strlen(str) ; i++)  /* Set loop to size of string        */
  78.    {
  79.       movedata( buf_seg, buf_off+i, SCR_SEG, scr_off + ( i * 2 ), 1);
  80.                     /* Copy data to screen memory        */
  81.       movedata( attr_seg, attr_off, SCR_SEG, scr_off + ( ( i * 2 ) + 1 ), 1);
  82.                     /* Copy attribute to screen memory   */
  83.    }
  84.    return(cnt);                     /* Return number of bytes (data) printed */
  85. }
  86.  
  87. int Save_Scr(int Top_Col, int Top_Row, int Bot_Col, int Bot_Row, char *Stor_Buf)
  88. {
  89.  
  90.    void far *address;                   /* Physical Address of Data Buffer   */
  91.    unsigned BUF_seg, BUF_off;
  92.    char BUF[(COL*2)+10];                /* Buffer to build sting into        */
  93.    int i, scr_off, Rows, Cols;
  94.  
  95.    if( Top_Col < 1 || Bot_Col > COL )   /* Check for legal screen values     */
  96.       return(-1);
  97.    if( Top_Row < 1 || Bot_Row > ROW )
  98.       return(-1);
  99.  
  100.    if(Bot_Col < Top_Col)                /* Check for legal values            */
  101.       return(-1);
  102.    if(Bot_Row < Top_Row)
  103.       return(-1);
  104.  
  105.    address = ( void far *)BUF;        /* Determine physical address (buffer) */
  106.    BUF_seg = FP_SEG(address);           /* Get segment (buffer)              */
  107.    BUF_off = FP_OFF(address);           /* Get offset (buffer)               */
  108.  
  109.    Rows = ( Bot_Row - Top_Row ) + 1;    /* Compute number of rows to save    */
  110.    Cols = (( Bot_Col - Top_Col ) + 1 ) * 2;
  111.                     /* Get lenght of each row (char&attr)*/
  112.    Stor_Buf[0] = NULL;                  /* Clear Storage buffer              */
  113.  
  114.    for( i = 0 ; i < Rows ; i++ )
  115.    {
  116.       scr_off=(( Top_Col * 2 ) - 2 ) + ((( Top_Row + i ) * ( COL * 2 )) - ( COL * 2 ));
  117.                     /* Compute offset of row             */
  118.       movedata( SCR_SEG, scr_off, BUF_seg, BUF_off, Cols );
  119.                     /* Move row into buffer memory       */
  120.       BUF[Cols] = NULL;                 /* Mark end of row                   */
  121.       strcat( Stor_Buf, BUF );          /* Move row into storage memory      */
  122.    }
  123.  
  124.    return(0);
  125. }
  126.  
  127. int Load_Scr(int Top_Col, int Top_Row, int Bot_Col, int Bot_Row, char *Stor_Buf)
  128. {
  129.  
  130.    void far *address;                   /* Physical Address of Data Buffer   */
  131.    unsigned BUF_seg, BUF_off;
  132.    int i, scr_off, Rows, Cols;
  133.  
  134.    if( Top_Col < 1 || Bot_Col > COL )   /* Check for legal screen values     */
  135.       return(-1);
  136.    if( Top_Row < 1 || Bot_Row > ROW )
  137.       return(-1);
  138.  
  139.    if(Bot_Col < Top_Col)                /* Check for legal box values        */
  140.       return(-1);
  141.    if(Bot_Row < Top_Row)
  142.       return(-1);
  143.  
  144.    address = ( void far *)Stor_Buf;     /* Determine physical address (buffer) */
  145.    BUF_seg = FP_SEG(address);           /* Get segment (buffer)              */
  146.    BUF_off = FP_OFF(address);           /* Get offset (buffer)               */
  147.  
  148.    Rows = ( Bot_Row - Top_Row ) + 1;    /* Number of rows to load            */
  149.    Cols = (( Bot_Col - Top_Col ) + 1 ) * 2;
  150.                     /* Length of each row (char + attr)  */
  151.  
  152.    for( i = 0 ; i < Rows ; i++ )
  153.    {
  154.       scr_off=(( Top_Col * 2 ) - 2 ) + ((( Top_Row + i ) * ( COL * 2 )) - ( COL * 2 ));
  155.                     /* Compute offset of row             */
  156.       movedata( BUF_seg, BUF_off + ( Cols * i ), SCR_SEG, scr_off, Cols );
  157.                     /* Load row into screen memory       */
  158.    }
  159.  
  160.    return(0);
  161. }
  162.